/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.sprite.ship;

import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.core.AbstractSprite;
import hcmus.fit.master.sevensea.core.Constant;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Point;

/**
 * 
 * @author hvp
 */
public class Cannon extends AbstractSprite {
    public static List<List<Bitmap>> Resource;
    public int DestX;
    public int DestY;
    public Explosion Explosion;
    private double _tempX;
    private double _tempY;
    // public static SoundPlayer Sound;
    // public static Player Sound;
    public int State;

    public Cannon(Sea seamap, int x, int y) {
        super(seamap, x, y);
        setFrames(Resource.get(0));
        setNbOfFrame(getFrames().size());
        DestX = x;
        DestY = y;
        Explosion = new Explosion(seamap, x, y);
        _tempX = x * Constant.CELL_WIDTH * 1.0;
        _tempY = y * Constant.CELL_HEIGHT * 1.0;
        State = CannonState.Flying;
    }

    public void InitPosition(int x, int y) {
        setRealPos(new Point(x * Constant.CELL_WIDTH + Constant.CELL_WIDTH / 2
                - getFrames().get(0).getWidth() / 2, y * Constant.CELL_HEIGHT
                + Constant.CELL_HEIGHT / 2 - getFrames().get(0).getHeight() / 2));
        X = x;
        Y = y;
        _tempX = getRealPos().x;
        _tempY = getRealPos().y;
    }

    public void Move() {
        int deltaX = DestX - X;
        int deltaY = DestY - Y;
        // trai tren
        if (deltaX < 0 && deltaY < 0) {
            _tempX -= Constant.CannonMoveDeltaX;
            _tempY -= Constant.CannonMoveDeltaY;
        }
        // trai
        else if (deltaX < 0 && deltaY == 0) {
            _tempX -= Constant.CannonMoveDeltaX;
        }

        // trai duoi
        else if (deltaX < 0 && deltaY > 0) {
            _tempX -= Constant.CannonMoveDeltaX;
            _tempY += Constant.CannonMoveDeltaY;
        }

        // tren
        else if (deltaX == 0 && deltaY < 0) {
            _tempY -= Constant.CannonMoveDeltaY;
        }

        // duoi
        else if (deltaX == 0 && deltaY > 0) {
            _tempY += Constant.CannonMoveDeltaY;
        }

        // phai tren
        else if (deltaX > 0 && deltaY < 0) {
            _tempX += Constant.CannonMoveDeltaX;
            _tempY -= Constant.CannonMoveDeltaY;
        }
        // phai
        else if (deltaX > 0 && deltaY == 0) {
            _tempX += Constant.CannonMoveDeltaX;
        }

        // phai duoi
        else if (deltaX > 0 && deltaY > 0) {
            _tempX += Constant.CannonMoveDeltaX;
            _tempY += Constant.CannonMoveDeltaY;
        }
        setRealPos(new Point((int) Math.floor(_tempX), (int) Math.floor(_tempY)));
    }

    public boolean Hit() {
        if (getRealPos().x == DestX * Constant.CELL_WIDTH + Constant.CELL_WIDTH / 2
                - getFrames().get(0).getWidth() / 2)
            if (getRealPos().y == DestY * Constant.CELL_HEIGHT + Constant.CELL_HEIGHT / 2
                    - getFrames().get(0).getHeight() / 2) {
                return true;
            }
        return false;
    }

    public boolean update() {
        switch (State) {
            case CannonState.KillEnemyFlying:
                if (Hit()) {
                    State = CannonState.KillEnemyHitted;
                } else {
                    Move();
                }
                break;
            case CannonState.KillEnemyHitted:
                getSeaMap().items.remove(this);
                if (getSeaMap().map[DestY][DestX] == null
                        || getSeaMap().map[DestY][DestX].isWhirl()) {
                    getSeaMap().createSplash(DestX, DestY);
                } else if (getSeaMap().map[DestY][DestX].isShip()) {
                    if (getSeaMap().map[DestY][DestX] != getSeaMap().mainShip) {
                        getSeaMap().boardControl._Score.SCORE += ((Ship) getSeaMap().map[DestY][DestX])
                                .GetScore();
                        getSeaMap().items.remove(getSeaMap().map[DestY][DestX]);
                        getSeaMap().map[DestY][DestX] = null;
                        getSeaMap().createRubble(DestX, DestY);
                        getSeaMap().createExplosion(DestX, DestY);
                    }
                }
                getSeaMap().busy--;
                if (getSeaMap().busy == 1) {
                    getSeaMap().enemyMove();
                }
                // return false;
                break;
            case CannonState.Flying:
                if (Hit()) {
                    State = CannonState.Hitted;
                } else {
                    Move();
                }
                break;
            case CannonState.Hitted:
                getSeaMap().items.remove(this);
                if (getSeaMap().map[DestY][DestX] == null
                        || getSeaMap().map[DestY][DestX].isWhirl()) {
                    getSeaMap().createSplash(DestX, DestY);
                } else if (getSeaMap().map[DestY][DestX].isShip()) {
                    getSeaMap().items.remove(getSeaMap().map[DestY][DestX]);
                    getSeaMap().map[DestY][DestX] = null;
                    getSeaMap().createRubble(DestX, DestY);
                    getSeaMap().createExplosion(DestX, DestY);
                }
                getSeaMap().busy--;
                break;
        }
        return true;
    }
}
